<template>
  <div class="container">
    <div class="app-container">
      <el-tree
        default-expand-all
        :data="data"
      >
        <el-row slot-scope="{data}" style="width:100%;" type="flex" justify="apace-around">
          <el-col>{{ data.name }}</el-col>
          <el-col :span="4">
            <el-row type="flex">
              <el-col :span="12">{{ data.managerName }}</el-col>
              <el-dropdown @command="operateDept($event,data.id)">
                <span>
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-row>
          </el-col>
        </el-row>
      </el-tree>
      <add-dept ref="addDept" :show-dialog.sync="showDialog" :parent-id="currentId" @updateList="initData" />
    </div>
  </div>
</template>
<script>
import { transListToTreeData } from '@/utils/index'
import { delDepartmentApi, getDepartmentListApi } from '@/api/department'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      showDialog: false,
      data: [],
      currentId: ''
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      const res = await getDepartmentListApi()
      // console.log(res)
      this.data = transListToTreeData(res, 0)
    },
    async operateDept(type, rowId) {
      this.currentId = rowId
      if (type === 'add') {
        this.showDialog = true
      }
      if (type === 'edit') {
        await this.$refs.addDept.reviewFormData(rowId)
        this.showDialog = true
      }
      if (type === 'del') {
        await this.$confirm('确认删除吗?')
        await delDepartmentApi(rowId)
        this.$message.success('删除成功')
        this.initData()
      }
    }
  }
}
</script>
<style scoped>
.app-container{
  padding: 50px 100px;
}
</style>
